'****************************************************************************'
'                                                                            '
' Copyright (C) 2010 Muhammad alaa                                           '
'                                                                            '
' This program is free software; you can redistribute it and/or modify       '
' it under the terms of the GNU General Public License as published by       '
' the Free Software Foundation; either version 2 of the License, or          '
' (at your option) any later version.                                        '
'                                                                            '
' This program is distributed in the hope that it will be useful,            '
' but WITHOUT ANY WARRANTY; without even the implied warranty of             '
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              '
' GNU General Public License for more details.                               '
'                                                                            '
' You should have received a copy of the GNU General Public License along    '
' with this program; if not, write to the Free Software Foundation, Inc.,    '
' 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                '
'                                                                            '
'****************************************************************************'

Namespace DataTypes

    ''' <summary>
    ''' Windows HResult
    ''' </summary>
    ''' <remarks></remarks>
    Public Structure HResult
        Private hr As Integer

        ''' <summary>
        ''' initialize new HResult
        ''' </summary>
        ''' <param name="value">an HResult number</param>
        Public Sub New(ByVal value As Integer)
            hr = value
        End Sub

        ''' <summary>
        ''' initialize new HResult
        ''' </summary>
        ''' <param name="value">an HResult object</param>
        Public Sub New(ByVal value As HResult)
            hr = value.hr
        End Sub

        ''' <summary>
        ''' create new HResult
        ''' </summary>
        ''' <param name="severity">Severity code</param>
        ''' <param name="facility">Facility code</param>
        ''' <param name="code">Error code</param>
        Public Sub New(ByVal severity As UShort, ByVal facility As UShort, ByVal code As UShort)
            hr = (CInt(severity) << 31) Or (CInt(facility) << 16) Or CInt(code)
        End Sub

        ''' <summary>
        ''' get hresult error code
        ''' </summary>
        Public ReadOnly Property Code() As Integer
            Get
                Return hr And &HFFFF
            End Get
        End Property

        ''' <summary>
        ''' get facility code
        ''' </summary>
        Public ReadOnly Property Facility() As Integer
            Get
                Return (hr >> 16) And &H1FFF
            End Get
        End Property

        ''' <summary>
        ''' get severity code
        ''' </summary>
        Public ReadOnly Property Severity() As Integer
            Get
                Return (hr >> 31) And &H1
            End Get
        End Property

        ''' <summary>
        ''' Is Failed
        ''' </summary>
        Public ReadOnly Property Failed() As Boolean
            Get
                Return hr < 0
            End Get
        End Property

        ''' <summary>
        ''' Is Succeeded
        ''' </summary>
        Public ReadOnly Property Succeeded() As Boolean
            Get
                Return hr >= 0
            End Get
        End Property

        ''' <summary>
        ''' Is HResult Failed
        ''' </summary>
        Public Shared Function IsFailed(ByVal value As HResult) As Boolean
            Return value.hr < 0
        End Function

        ''' <summary>
        ''' Is HResult Succeeded
        ''' </summary>
        Public Shared Function IsSucceeded(ByVal value As HResult) As Boolean
            Return value.hr >= 0
        End Function

        ''' <summary>
        ''' get HResult from NT Error code
        ''' </summary>
        ''' <param name="value">NT Error code</param>
        ''' <returns>HResult Object</returns>
        Public Shared Function FromNT(ByVal value As Integer) As HResult
            Return value Or &H10000000
        End Function

        ''' <summary>
        ''' get HResult from Win32 Error code
        ''' </summary>
        ''' <param name="value">Win32 Error code</param>
        ''' <returns>HResult Object</returns>
        Public Shared Function FromWin32(ByVal value As Integer) As HResult
            Return If(value <= 0, value, ((value And &HFFFF) Or (7 << 16) Or &H80000000))
        End Function

        ''' <summary>
        ''' is passed hresult is an error
        ''' </summary>
        ''' <param name="value">hresult to check</param>
        ''' <returns>true if it is, false otherwise</returns>
        Public Shared Function IsError(ByVal value As HResult) As Boolean
            Return value.hr >> 31 = 1
        End Function

#Region "Conversion Operator"
        Public Shared Widening Operator CType(ByVal value As Integer) As HResult
            Return New HResult(value)
        End Operator

        Public Shared Widening Operator CType(ByVal value As HResult) As Integer
            Return value.hr
        End Operator
#End Region

#Region "Compare Operators"
        Public Shared Operator =(ByVal hr1 As HResult, ByVal hr2 As HResult) As Boolean
            Return hr1.hr = hr2.hr
        End Operator

        Public Shared Operator <>(ByVal hr1 As HResult, ByVal hr2 As HResult) As Boolean
            Return hr1.hr <> hr2.hr
        End Operator
#End Region

    End Structure

End Namespace